package com.monitor.control;

import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 上网时间控制器，用于管理允许上网的时间段
 */
public class TimeController {
    // 存储每天的允许上网时间段
    private Map<DayOfWeek, TimeRange[]> allowedTimeRanges;
    private boolean enabled = true;
    
    public TimeController() {
        allowedTimeRanges = new HashMap<>();
        initDefaultTimeRanges();
    }
    
    private void initDefaultTimeRanges() {
        // 默认设置：周一至周五 8:00-12:00, 14:00-18:00，周末 9:00-20:00
        TimeRange[] weekdayRanges = {
            new TimeRange(LocalTime.of(8, 0), LocalTime.of(12, 0)),
            new TimeRange(LocalTime.of(14, 0), LocalTime.of(18, 0))
        };
        
        TimeRange[] weekendRanges = {
            new TimeRange(LocalTime.of(9, 0), LocalTime.of(20, 0))
        };
        
        // 设置工作日时间范围
        allowedTimeRanges.put(DayOfWeek.MONDAY, weekdayRanges);
        allowedTimeRanges.put(DayOfWeek.TUESDAY, weekdayRanges);
        allowedTimeRanges.put(DayOfWeek.WEDNESDAY, weekdayRanges);
        allowedTimeRanges.put(DayOfWeek.THURSDAY, weekdayRanges);
        allowedTimeRanges.put(DayOfWeek.FRIDAY, weekdayRanges);
        
        // 设置周末时间范围
        allowedTimeRanges.put(DayOfWeek.SATURDAY, weekendRanges);
        allowedTimeRanges.put(DayOfWeek.SUNDAY, weekendRanges);
    }
    
    /**
     * 检查当前时间是否允许上网
     * @return 如果当前时间允许上网返回true，否则返回false
     */
    public boolean isInternetAllowed() {
        if (!enabled) {
            return true; // 如果控制器被禁用，则始终允许上网
        }
        
        LocalDateTime now = LocalDateTime.now();
        DayOfWeek today = now.getDayOfWeek();
        LocalTime currentTime = now.toLocalTime();
        
        // 获取今天的允许时间范围
        TimeRange[] todayRanges = allowedTimeRanges.get(today);
        if (todayRanges == null) {
            return false; // 如果没有设置今天的时间范围，则不允许上网
        }
        
        // 检查当前时间是否在允许范围内
        for (TimeRange range : todayRanges) {
            if (range.contains(currentTime)) {
                return true;
            }
        }
        
        return false;
    }
    
    /**
     * 检查是否即将到达禁网时间（5分钟内）
     * @return 如果即将到达禁网时间返回true，否则返回false
     */
    public boolean isApproachingBlockTime() {
        if (!enabled) {
            return false;
        }
        
        LocalDateTime now = LocalDateTime.now();
        DayOfWeek today = now.getDayOfWeek();
        LocalTime currentTime = now.toLocalTime();
        
        // 获取今天的允许时间范围
        TimeRange[] todayRanges = allowedTimeRanges.get(today);
        if (todayRanges == null) {
            return false;
        }
        
        // 检查是否在任一时间范围内且距离结束时间不到5分钟
        for (TimeRange range : todayRanges) {
            if (range.contains(currentTime)) {
                LocalTime endTime = range.getEndTime();
                LocalTime warningTime = endTime.minusMinutes(5);
                
                if (currentTime.isAfter(warningTime) || currentTime.equals(warningTime)) {
                    return true;
                }
            }
        }
        
        return false;
    }
    
    /**
     * 设置指定星期几的允许上网时间范围
     * @param dayOfWeek 星期几
     * @param ranges 时间范围数组
     */
    public void setAllowedTimeRanges(DayOfWeek dayOfWeek, TimeRange[] ranges) {
        allowedTimeRanges.put(dayOfWeek, ranges);
    }
    
    /**
     * 获取指定星期几的允许上网时间范围
     * @param dayOfWeek 星期几
     * @return 时间范围数组
     */
    public TimeRange[] getAllowedTimeRanges(DayOfWeek dayOfWeek) {
        return allowedTimeRanges.get(dayOfWeek);
    }
    
    /**
     * 检查时间控制器是否启用
     * @return 如果启用返回true，否则返回false
     */
    public boolean isEnabled() {
        return enabled;
    }
    
    /**
     * 启用时间控制器
     */
    public void enable() {
        this.enabled = true;
    }
    
    /**
     * 禁用时间控制器
     */
    public void disable() {
        this.enabled = false;
    }
    
    /**
     *
     * 
     **/
}